﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DAO;

namespace TAM_SPORT
{
    public partial class SalesWithDebitReport : TAM_SPORT.FormBase
    {
        private SalesDao dao;
        public SalesWithDebitReport()
        {
            InitializeComponent();
            dao = new SalesDao();
            loadGrid();
            cbxStattus.SelectedIndex = 0;
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            loadGrid();
        }

        private void loadGrid()
        {
            DataTable table = dao.getDebitCustomer(txtName.Text.Trim(), cbxStattus.SelectedIndex,  dtFromDate.Value, dtpToDate.Value);
            if (cbxStattus.SelectedIndex == 0)
            {
                clCollect.Visible = true;
            }
            else
            {
                clCollect.Visible = false;
            }
            dtgCustomers.Rows.Clear();
            double debitTotal = 0;
            if (table != null && table.Rows.Count > 0)
            {
                foreach (DataRow row1 in table.Rows)
                {
                    debitTotal += Convert.ToDouble(row1["DEBIT_MONEY"]);
                    int i = dtgCustomers.Rows.Add();
                    dtgCustomers.Rows[i].Cells[0].Value = i + 1;
                    dtgCustomers.Rows[i].Cells[1].Value = row1["NAME"].ToString();
                    dtgCustomers.Rows[i].Cells[2].Value = Convert.ToDouble(row1["DEBIT_MONEY"]).ToString("#,###.##");
                    dtgCustomers.Rows[i].Cells[4].Value = row1["CUSTOMER_ID"].ToString();
                    if (i % 2 == 1)
                    {
                        dtgCustomers.Rows[i].DefaultCellStyle.BackColor = Color.LightCyan;
                    }
                }
            }
            txtTotal.Text = debitTotal.ToString("#,###.##");
        }

        private void btnView_Click_1(object sender, EventArgs e)
        {
            loadGrid();
        }

        private void dtgCustomers_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex == 3)
            {
                int id = int.Parse(dtgCustomers.Rows[e.RowIndex].Cells[4].Value.ToString());
                DebitColecttion con = new DebitColecttion(id);
                con.ShowDialog();
                con.Dispose();
                loadGrid();
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            Export();
        }

        private void Export()
        {
            saveFile.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*";

            string fileName = "";

            if (saveFile.ShowDialog() == DialogResult.OK)
            {
                fileName = saveFile.FileName;
            }
            ExcelManager excelManager = new ExcelManager();
            if (!string.IsNullOrEmpty(fileName))
            {
                excelManager.ExportDebitCustomer(txtName.Text.Trim(), cbxStattus.SelectedIndex,  
                    dtFromDate.Value, dtpToDate.Value, fileName);
            }
            saveFile.FileName = null;
        }

    }
}
